<?php


namespace App\Utils\Service\Syj;


use App\Models\Syj\cs;
use Carbon\Carbon;

class LiveService
{
    private $config;
    public function __construct()
    {
        $this->config = [
            'PushDomain'=>'177216.push.tlivecloud.com',//推流地址
            'LiveDomain'=>'chneyu.yusl.xyz.tlivecdn.com',//播流地址
            'PushKey'=>'f06311689689c41f4ea6645ca6eac031',//推流Key
            'LiveKey'=>'STpCRtQn4wZxe3WjxTHw',//播流Key
            'time'=>Carbon::now()->addDay()
        ];
    }
    //推流
    function PushUrl($name){
        //查询直播间名称
        $live = cs::findName($name);
        if ($live) {
            return $this->getLiveUrl($this->config['PushDomain'], $live[0]['id'] . $live[0]['name'], $this->config['PushKey'], $this->config['time']);
        }
        $data= cs::setLive(['name'=>$name]);
        return $this->getLiveUrl($this->config['PushDomain'],$data->id.$data->name,$this->config['key'],$this->config['time']);
    }
    //播流
    function LiveUrl($name){
        $live = cs::findName($name);
        //dd($live);
        return   $this->getLiveUrl($this->config['LiveDomain'],$live[0]['id'].$live[0]['name'],$this->config['LiveKey'],$this->config['time']);
    }
    //推流&播流
    private function getLiveUrl($domain, $streamName, $key = null, $time = null){
        if($key && $time){
            //strtoupper把所有字符转换为大写
            $txTime = strtoupper(base_convert(strtotime($time),10,16));
            $txSecret = md5($key.$streamName.$txTime);
            $ext_str = "?".http_build_query(array(
                    "txSecret"=> $txSecret,
                    "txTime"=> $txTime
                ));
            //预生产环境-》生成环境
            //禅道
        }
        return "webrtc://".$domain."/live/".$streamName . (isset($ext_str) ? $ext_str : "");
    }
}
